package portal.action.rest;

import beans.HttpResultList;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import portal.beans.HostGroup;
import portal.mybatis.dao.service.HostGroupService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class HostGroupAction {
    @Autowired
    HostGroupService hostGroupService;
    private HttpResultList result;

    @RequestMapping(value ="/HostGroupServlet",method = RequestMethod.POST)
    public void HostGroupService(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String operateType = request.getParameter("operateType");
        try {
            switch (operateType){
                case "add":
                    addHostGroup(request,response);
                    break;
                case "delete":
                    deleteHostGroup(request,response);
                    break;
                case "alter":
                    alterHostGroup(request,response);
                    break;
                case "showAll":
                    showAllHostGroup(request,response);
                    break;
                case "showPaged":
                    showPaged(request,response);
                    break;
                case "showDetail":
                    showHostGroupDetail(request,response);
                    break;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST参数错误");
            response.getWriter().print(JSONObject.fromObject(result).toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST数字错误");
        }
        response.getWriter().print(JSONObject.fromObject(result).toString());
    }

    private void addHostGroup(HttpServletRequest request, HttpServletResponse response) {
        String hostGroupName = request.getParameter("hostGroupName");
        String hostGroupIntroduction = request.getParameter("hostGroupIntroduction");
        HostGroup hostGroup = new HostGroup(hostGroupName, hostGroupIntroduction);
        result = new HttpResultList();
        boolean res;
        if (hostGroupService.insertHostGroup(hostGroup) == 1) {
            res = true;
        }
        else {
            res = false;
        }
        result.setResult(res);
    }

    private void deleteHostGroup(HttpServletRequest request, HttpServletResponse response) {
        Integer hostGroupId = Integer.parseInt(request.getParameter("hostGroupId"));
        result = new HttpResultList();
        boolean res;
        if (hostGroupService.deleteById(hostGroupId) == 1) {
            res = true;
        }
        else {
            res = false;
        }
        result.setResult(res);
    }

    private void alterHostGroup(HttpServletRequest request, HttpServletResponse response) {
        Integer hostGroupId = Integer.parseInt(request.getParameter("hostGroupId"));
        String hostGroupName = request.getParameter("hostGroupName");
        String hostGroupIntroduction = request.getParameter("hostGroupIntroduction");
        HostGroup hostGroup = new HostGroup(hostGroupId, hostGroupName, hostGroupIntroduction);
        result = new HttpResultList();
        boolean res;
        if (hostGroupService.updateHostGroupById(hostGroup) == 1){
            res = true;
        }
        else {
            res = false;
        }
        result.setResult(res);
    }

    private void showAllHostGroup(HttpServletRequest request, HttpServletResponse response) {
        result = new HttpResultList();
        List<HostGroup> hostGroupList = hostGroupService.showAll();
        List<Object> objects = new ArrayList<>();
        for(int i=0; i<hostGroupList.size(); i++){
            objects.add(hostGroupList.get(i));
        }
        result.setResult(hostGroupList.size()>0);
        result.setDatas(objects);
    }

    private void showPaged(HttpServletRequest request, HttpServletResponse response) {
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        List<HostGroup> hostGroupList = hostGroupService.showPaged(currentPage, onePageCount);
        result = new HttpResultList();
        List<Object> objects = new ArrayList<>();
        for(int i=0; i<hostGroupList.size(); i++){
            objects.add(hostGroupList.get(i));
        }
        result.setResult(hostGroupList.size()>0);
        result.setDatas(objects);
        if(onePageCount!=0&&currentPage!=0){
            Long allCount = hostGroupService.countAll();
            double res = Math.ceil((double)allCount / (double)onePageCount);
            result.setMessage((int)res+"");
        }
    }

    private void showHostGroupDetail(HttpServletRequest request, HttpServletResponse response) {
        Integer hostGroupId = Integer.parseInt(request.getParameter("hostGroupId"));
        List<HostGroup> hostGroupList = hostGroupService.selectById(hostGroupId);
        result = new HttpResultList();
        List<Object> objects = new ArrayList<>();
        for(int i=0; i<hostGroupList.size(); i++){
            objects.add(hostGroupList.get(i));
        }
        result.setDatas(objects);
        result.setResult(hostGroupList.size()>0);
    }
}
